/* UniApo: SQLGenerator
 * filename:  main.cpp
 * date:      August 2010
 * author:    Jeffrey Geyssens
 * about:     This module all takes a PDS file as input and generates
 *            SQL statements from the results of a parsing bout.
 * usage:     Parser -b pname x.MAP (creates a new parser called pname).
 *            Parser -c (shows current parser).
 *            Parser -s x (sets current parser to x).
 *            Parser -p x.SDS (parse SDS and display PMAP results).
 *
 *            As is the case in all UniApo file editors:
 *              h: displays hardcoded help content.
 *              s: builds file from objects and saves to disk.
 *              q: quit and return to OS.
 *
 * Marked as completed: 28th june 2010 and no changes since
 */
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include "../UniApoLib/PDSFile.h"

using namespace std;

int main(int argc, char** argv) {

    //The SQL Generator has just one form of usage
    //SQLGenerator x.PDS savefile
    if (argc != 3) {
        cerr << "ERROR (SQLGenerator): Requires two arguments" << endl;
        return 90;
    }

    void processLine(string const & lineData);
    //Declare the variables derived from the command line
    string PDSfile = argv[1];
    string savefile = argv[2];
    //Get different parts of PDS file name
    string PDSfilename = PDSfile.substr(0, PDSfile.size() - 4);
    string PDSextension = PDSfile.substr(PDSfile.size() - 4, PDSfile.size());
    //Return error if the .PDS extension is missing.
    if (PDSextension != ".PDS") {
        cerr << "ERROR (Parser): ";
        cerr << "Expected first argument with PDS extension." << endl;
        return 91;
    }
    //Create an empty PDS object and give it the argument name
    PDSFile PDS;
    PDS.name(PDSfilename);
    //Load the PDS with the given filename
    PDS.loadFile();

    //Display Interface
    PDS.print();

    cout << "SQL Generator" << endl;
    string mod;
    while (mod != "q") {//while not quit
        //enter commands
        cout << "Enter command (g,s,r,q or h for help): ";
        getline(cin, mod);
        //Build and save file
        if (mod == "h") {
            cout << "---SQL Generator Help---" << endl;
            cout << "The interface provides an overview of the results ";
            cout << "generated by the parser." << endl;
            cout << "The numerical values next to the parsed entry ";
            cout << "represent the current status of SQL generation:" << endl;
            cout << "0: SQL generation success;" << endl;
            cout << "1: Syntax is accepted but the storage rules for ";
            cout << "the given data string structure have yet to be defined;";
            cout << endl;
            cout << "2: Bad syntax: The parser used to translate the sentence";
            cout << " does not contain the needed syntax rules to expand the";
            cout << "data string;" << endl << endl;
            cout << "Press s to save SQL statements to file;" << endl;
            cout << "Press g for generated SQL statements" << endl;
            cout << "Press t to test generated SQL statements on test db";
            cout << endl;
            cout << "Press r to run SQL statements on storage DB" << endl;
            cout << "Press a to add Storage Rules to MAP file" << endl;
        }

        if (mod == "g") {
            cout << "Submit entry number: ";
            int entryNo;
            string eline;
            getline(cin, eline);
            if (eline != "#") {
                stringstream ss(eline);
                ss >> entryNo;

                if (PDS.sqlstatus(entryNo) == 2) {
                    cout << "Bad Syntax: The parser used to parse this data ";
                    cout << "string does not support it's syntax" << endl;
                }
                if (PDS.sqlstatus(entryNo) == 1) {
                    cout << "Starting Learner" << endl;
                    cout << "Warning: Using the learner alters the MAP file";
                    cout << " which was used to create the parser" << endl;
                    cout << "The string was parsed as followed: " << endl;
                    PDS.printResult(entryNo);
                }

                if (PDS.sqlstatus(entryNo) == 0) {
                    cout << "SQL Statements available" << endl;
                    cout << "Select SQL Statement type:" << endl;
                    cout << "Use d to display the parameterized SQL statements";
                    cout << " which serve as the source of the statement";
                    cout << endl;
                    cout << "Use v to display dummy statement which displays ";
                    cout << "how the data values derived from data string are ";
                    cout << "arranged in the SQL statement" << endl;
                    cout << "Use r to display how the statement looks after ";
                    cout << "replacing data values with their database key";
                    cout << endl;
                    string choice = "a";

                    while (choice != "q") {
                        cout << "Submit SQL view (or q to quit): ";
                        getline(cin, choice);
                        if (choice == "v") {
                            PDS.getValSQL(entryNo);
                        }
                        if (choice == "d") {
                            PDS.getDisplaySQL(entryNo);
                        }

                        if (choice == "r") {
                            PDS.getPKSQL(entryNo);
                        }
                    }
                }
            }
        }

        if (mod == "s") {

        }





    }
    return 0;
}
